package zjs.dc.controller;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

/**
 * @Class:
 * @Author: xyl
 * @Description: 针对企查查 t_register 表和中数 t_case_filing 表进行数据映射开发
 * @Date: 2021/12/15 16:51
 */
public class TRegister {
    public static void main(String[] args) {
        SparkSession spark = SparkSession.builder().appName("t_register").enableHiveSupport().getOrCreate();
        spark.sparkContext().setLogLevel("ERROR");
        Dataset<Row> zs_data = spark.sql("select * from a_dc_ep_ods.zs_t_case_filing_base");
        long zs_count = zs_data.count();
        if(zs_count>0){

            /**
             * t_register
             */
            //中数删除逻辑：此表需要使用“案号”来删除
            Dataset<Row> delete_data = spark.sql("select a.id," +
                    "a.key_no," +
                    "a.company_id," +
                    "a.companyname," +
                    "a.register_id," +
                    "a.case_no," +
                    "a.punish_date," +
                    "a.prosecutor," +
                    "a.appellee," +
                    "b.jobid," +
                    "'-1' as isadd  " +
                    "from (select * from a_dc_ep_dwi.t_register where case_no is not null and case_no !='')a  " +
                    "inner join (select * from a_dc_ep_ods.zs_t_case_filing_base_del where CASECODE is not null and CASECODE !='') b  " +
                    "on upper(trim(translate(translate(a.case_no,\"（\",\"(\"),\"）\",\")\"))) = upper(trim(translate(translate(b.CASECODE,\"（\",\"(\"),\"）\",\")\"))) ");
            delete_data.createOrReplaceTempView("t_register_temp");
            spark.sql("insert overwrite table a_dc_ep_incr.t_register select * from t_register_temp");

            //原告被告临时表
            Dataset<Row> yuangao_beigao_data = spark.sql("select \n" +
                    "  b.docid,\n" +
                    "  translate(concat_ws('|',collect_list(if(b.type='原告',b.names,''))),\"|\",\"\") as pro,\n" +
                    "  translate(concat_ws('|',collect_list(if(b.type='被告',b.names,''))),\"|\",\"\") as app \n" +
                    "from \n" +
                    "  (select \n" +
                    "    a.docid,a.type type ,concat_ws(',',collect_list(a.name)) names \n" +
                    "  from \n" +
                    "    (SELECT \n" +
                    "      docid,\n" +
                    "      name,\n" +
                    "      if(trim(TYPE_NAME) in('公诉人','原告','上诉人','申请人'),'原告',if(trim(TYPE_NAME) in('被告人','被告','被上诉人','被申请人'),'被告','其他')) as type \n" +
                    "    FROM a_dc_ep_ods.zs_t_case_filing_party)a \n" +
                    "  group by a.docid,a.type)b \n" +
                    "group by b.docid");
            yuangao_beigao_data.createOrReplaceTempView("t_yuangao_beigao_temp");

            //更新数据--“案号”关联
            Dataset<Row> update_data = spark.sql("SELECT " +
                    "d.id," +
                    "d.key_no," +
                    "d.company_id," +
                    "d.companyname," +
                    "d.register_id," +
                    "b.casecode as case_no," +
                    "b.regdate as punish_date," +
                    "e.pro," +
                    "e.app," +
                    "b.jobid," +
                    "'0' as isadd " +
                    "FROM " +
                    "  (SELECT * FROM a_dc_ep_ods.zs_t_case_filing_party WHERE CREDITCODE IS NOT NULL AND CREDITCODE !='' and docid IS NOT NULL AND docid !='') a " +
                    "left JOIN " +
                    "  (SELECT * FROM t_yuangao_beigao_temp) e " +
                    "on a.docid=e.docid " +
                    "INNER JOIN " +
                    "  (SELECT * FROM a_dc_ep_ods.zs_t_case_filing_base WHERE docid IS NOT NULL AND docid !='') b " +
                    "on a.docid=b.docid "+
                    "INNER JOIN " +
                    "  (SELECT * FROM a_dc_ep_ods.t_eci_company WHERE credit_code IS NOT NULL AND credit_code !='') c " +
                    "on a.CREDITCODE=c.credit_code " +
                    "INNER JOIN " +
                    "  (SELECT * FROM a_dc_ep_dwi.t_register WHERE case_no IS NOT NULL AND case_no !='') d " +
                    "on upper(trim(translate(translate(b.CASECODE,\"（\",\"(\"),\"）\",\")\"))) = upper(trim(translate(translate(d.case_no,\"（\",\"(\"),\"）\",\")\"))) and c.company_id=d.company_id " );
            update_data.createOrReplaceTempView("t_register_one");
            spark.sql("insert into table a_dc_ep_incr.t_register select * from t_register_one");

            //新增数据--“案号”关联
            Dataset<Row> insert_data = spark.sql("SELECT " +
                    "a.RECORD_ID," +
                    "c.key_no," +
                    "c.company_id," +
                    "c.company_name," +
                    "b.docid," +
                    "b.casecode as case_no," +
                    "b.regdate as punish_date," +
                    "e.pro as prosecutor," +
                    "e.app as appellee," +
                    "b.jobid," +
                    "'1' as isadd " +
                    "FROM " +
                    "  (SELECT * FROM a_dc_ep_ods.zs_t_case_filing_party WHERE CREDITCODE IS NOT NULL AND CREDITCODE !='' and docid IS NOT NULL AND docid !='') a " +
                    "left JOIN " +
                    "  (SELECT * FROM t_yuangao_beigao_temp) e " +
                    "on a.docid=e.docid " +
                    "INNER JOIN " +
                    "  (SELECT * FROM a_dc_ep_ods.zs_t_case_filing_base WHERE docid IS NOT NULL AND docid !='') b " +
                    "on a.docid=b.docid "+
                    "INNER JOIN " +
                    "  (SELECT * FROM a_dc_ep_ods.t_eci_company WHERE credit_code IS NOT NULL AND credit_code !='') c " +
                    "on a.CREDITCODE=c.credit_code " +
                    "left JOIN " +
                    "  (SELECT * FROM a_dc_ep_dwi.t_register WHERE case_no IS NOT NULL AND case_no !='') d " +
                    "on upper(trim(translate(translate(b.CASECODE,\"（\",\"(\"),\"）\",\")\"))) = upper(trim(translate(translate(d.case_no,\"（\",\"(\"),\"）\",\")\"))) " +
                    "where d.case_no is null ");
            insert_data.createOrReplaceTempView("t_register_two");
            spark.sql(" insert into table a_dc_ep_incr.t_register select * from t_register_two");
        }else {
            spark.sql("TRUNCATE TABLE a_dc_ep_incr.t_register");
            System.out.println("中数本期数据涉及该表的数据为空。。。。。。");
        }
        spark.stop();
    }
}
